<?php
namespace app\home\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

/**
 *
 * 支付页面
 */
class Recharge extends Base{

    //线上支付页面
	public function recharge(){
        //测试uid——————————————————————————————————————————————————————————————————————————————————————
        //Session::set('uid','1561');
        // var_dump(Session::get('uid'));die;
    //线上支付种类
          $payments=Db::name('payments')->where('enabled','1')->order('payOrder asc')->select();
        //最低金额
         $min=Db::name('config')->where('id=3')->field('value')->find();
         $min=$min['value'];
         //var_dump($payments);
         // var_dump($min);
        $this->assign('payments',$payments);
        $this->assign('min',$min);
		return $this->fetch('Recharge/online_recharge');
	}

    //是否生成订单
    public function  order(){
        $data=$this->request->post();
        $data=myarr($data);
     if($data['typepay']==NULL){
            return "<script>alert('请选项充值方式');window.location.href='".url('/home/Recharge/recharge')."'</script>";
        }
        //充值最低金额
        $min=Db::name('config')->where('key','czmin')->field('value')->find();
        $min=$min['value'];
        //是否是非法金额
     if(preg_match("/[\',:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$data['amount'])){
         return "<script>alert('请输入正确的金额');window.location.href='".url('/home/Recharge/recharge')."'</script>";
         }
        if($data['amount']<$min){
            return "<script>alert('充值金额必须大于{$min}元');window.location.href='".url('/home/Recharge/recharge')."'</script>";;
        }
        //手续费
        $list=Db::name('payments')->where('id',$data['typepay'])->find();
        //手续费
        $data['charge']=round($data['amount']*$list['ServiceCharge'],2);
        //总金额
        $data['zamount']=round($data['amount']+$data['charge'],2);
        $data['payName']=$list['payName'];
        $data['paymentCode']=$list['id'];
        //var_dump($data);
        $this->assign('data',$data);
        return $this->fetch('Recharge/temp');
    }
    //支付种类
    //线上支付
    public function insert(){

        $param = $this->request->get();
        $param=myarr($param);
        //数据是否非法
        if(preg_match("/[\',:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$param['amount'])){
            return "<script>alert('充值错误，请重新充值！');window.location.href='".url('/home/Recharge/recharge')."'</script>";
        }
        if(preg_match("/[\',:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$param['charge'])){
            return "<script>alert('充值错误，请重新充值！');window.location.href='".url('/home/Recharge/recharge')."'</script>";
        }
        if(preg_match("/[\',:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$param['paymentCode'])){
            return "<script>alert('充值错误，请重新充值！');window.location.href='".url('/home/Recharge/recharge')."'</script>";
        }
        if($param['amount']<0){
            return "<script>alert('充值错误，请重新充值！');window.location.href='".url('/home/Recharge/recharge')."'</script>";
        }
        //流水号
        //充值总金额
         $param['amount'];
        //手续费
          $param['charge'];
        //充值金额
         $balance=$param['amount']-$param['charge'];
        //充值类型
        $param['paymentCode'];
        // 个人支付宝
        // $data['amount']=Db::name('pay')
        if($param['paymentCode']==1){

            if(($param['amount']*100)%100==0){
                return  "/home/Recharge/recharge";
            }
            $payType='alipay';
            $seller_email='15158037996';
            $price=$balance;
            $isMobile   ='0';
            $returnUrl ='';
            $_SESSION['skl_uid']=Session::GET('uid');
            //return "/shoukuanla/index.php?a=insert&payType=$payType&seller_email=$seller_email&price=$price&isMobile=$isMobile";
            // header("location:/shoukuanla/index.php?a=insert&payType=$payType&seller_email=$seller_email&price=$price&isMobile=$isMobile&returnUrl=$returnUrl");
            $url="/shoukuanla/index.php?a=insert&payType=$payType&seller_email=$seller_email&price=$price&isMobile=$isMobile&returnUrl=$returnUrl";
            $this->redirect($url);
        }

        //支付宝对公账户
        if($param['paymentCode']==2){
            //充值金额
            $amount=$balance;
            //总金额充值加手续费
            //return "/home/Alipays/getAlipaysUrl?amount=$amount";
            //header("location:/home/Alipays/getAlipaysUrl?amount=$amount");
            $url="/home/Alipays/getAlipaysUrl?amount=$amount";
            $this->redirect($url);
        }

        //杉德
        if($param['paymentCode']==3){

            $charge=Db::name('payments')->where('id',3)->field('ServiceCharge')->find();
            //充值金额的手续费
            $charge=round($balance*$charge['ServiceCharge'],2);
            //总金额
            $toamount=round($balance+$charge,2);
            $time=date('Y-m-d H:i:s');
            $arr=array();
            $arr['uid']=Session::get('uid');
            $arr['tradeNo']=orderNum();
            $arr['amount']=$balance;
            $arr['paymentCode']=3;
            $arr['offline']=1;
            $arr['paymentstate']=0;
            $arr['poundage']=$charge;
            $arr['addtime']=$time;
            $ste=Db::name('user_recharge')->insert($arr);
            if($ste){
                //12 位，例 000000000101 代表1.01 元
                /// $toamount=sprintf("%12d", $toamount*100);
                // return  "http://www.lczc999.com/sendpay/orderpay.php?InMerBillNo=".$order."&InAmount=".$toamount;
                //return  "/home/Recharge/sande?InMerBillNo=".$arr['tradeNo']."&InAmount=".$toamount."&charge=".$charge;
               // header("location:/home/Recharge/shande?InMerBillNo=".$arr['tradeNo']."&InAmount=".$toamount."&charge=".$charge);
                $url="/home/Recharge/shande?InMerBillNo=".$arr['tradeNo']."&InAmount=".$toamount."&charge=".$charge;
                $this->redirect($url);
            }else{
                //下单失败
                return  "<script>alert('请重试');window.location.href='".url('/home/Recharge/recharge')."'</script>";
            }

        }

//环迅支付
        if($param['paymentCode']==4){

            $charge=Db::name('payments')->where('id',4)->field('ServiceCharge')->find();
            //充值金额的手续费
            $charge=round($balance*$charge['ServiceCharge'],2);
            //总金额
            $toamount=round($balance+$charge,2);

            $time=date('Y-m-d H:i:s');
            $arr=array();
            $arr['uid']=Session::get('uid');
            $arr['tradeNo']=orderNum();
            $arr['amount']=$balance;
            $arr['paymentCode']=4;
            $arr['offline']=1;
            $arr['paymentstate']=0;
            $arr['poundage']=$charge;
            $arr['addtime']=$time;
            $ste=Db::name('user_recharge')->insert($arr);
            if($ste){
               // return  "http://www.lczc999.com/OrderPay/ipspayapi.php?InMerBillNo=".$order."&InAmount=".$toamount;
               // header("location:http://www.lczc999.com/OrderPay/ipspayapi.php?InMerBillNo=". $arr['tradeNo']."&InAmount=".$toamount);
                $url="http://www.lczc999.com/OrderPay/ipspayapi.php?InMerBillNo=". $arr['tradeNo']."&InAmount=".$toamount;
                $this->redirect($url);
            }else{
                //下单失败
                return  "<script>alert('请重试');window.location.href='".url('/home/Recharge/recharge')."'</script>";
            }

        }

        //银生
        if($param['paymentCode']==7){
            $charge = Db::name('payments')->field('ServiceCharge')->where('id=7')->find();
            $charges= $balance*$charge['ServiceCharge'];
            $tot = trim(round($balance+$charges,2));
            $time = date('YmdHis');
            $arr = [
                'uid'=>Session::get('uid'),
                'tradeNo'=>orderNum(),
                'amount'=>round($balance,2),
                'offline'=>1,//线上充值
                'poundage'=>round($charges,2),//手续费
                // 'remarks'=>'银生充值',
                'paymentCode'=>7,
               ];
            //var_dump($arr);die;
            $ins = Db::name('user_recharge')->insert($arr);
            if($ins){
                //return  "http://www.lczc999.com/Yinsheng/yinsheng.php?amount=".$tot."&makeSn=".$arr['tradeNo']."&time=".$time;
               // header("location:http://www.lczc999.com/Yinsheng/yinsheng.php?amount=".$tot."&makeSn=2525252527575&time=".$time);
                $url="http://www.lczc999.com/Yinsheng/yinsheng.php?amount=".$tot."&makeSn=".$arr['tradeNo']."&time=".$time;
                $this->redirect($url);
            }else{
                return  "<script>alert('请重试');window.location.href='".url('home/Recharge/recharge')."'</script>";
            }
        }
//杭州市名卡快捷支付
        if($param['paymentCode']==5){

            $toamount=$balance;
            //手续费
            $charge=Db::name('payments')->where('id=5')->field('ServiceCharge')->find();
            $charge=$toamount*$charge['ServiceCharge'];

            //var_dump('1111');
            //header("location:/home/Apaycard/uppage?toamount=".$toamount."&charge=".$charge);
            $url="/home/Apaycard/uppage?toamount=".$toamount."&charge=".$charge;
            $this->redirect($url);

        }

        if($param['paymentCode']==6){
        
            $amount=$balance;
            $charge=Db::name('payments')->where('id=6')->field('ServiceCharge')->find();
            $charge=round($amount*$charge['ServiceCharge'],2);
           /* var_dump($amount);
            var_dump($charge);die;*/
            //return "/home/Gateway/xiapay?amount=$amount&charge=$charge";
            $url="/home/Gateway/xiapay?amount=$amount&charge=$charge";
            $this->redirect($url);
        }

        //四方支付
        if($param['paymentCode']==8){

            $charge=Db::name('payments')->where('id',8)->field('ServiceCharge')->find();
//            dump($charge);die;
            //充值金额的手续费
            $charge=round($balance*$charge['ServiceCharge'],2);
            //总金额
            $toamount=round($balance+$charge,2);
            $time=date('Y-m-d H:i:s');
            $arr=array();
            $arr['uid']=Session::get('uid');
            $arr['tradeNo']=orderNum();
            $arr['amount']=$balance;
            $arr['paymentCode']=8;
            $arr['offline']=1;
            $arr['paymentstate']=0;
            $arr['poundage']=$charge;
            $arr['addtime']=$time;
            $ste=Db::name('user_recharge')->insert($arr);
            $ip=$_SERVER['REMOTE_ADDR'];
            if($ste){
                $url="http://www.rcw98.com/sipay/sifangpay.php?InMerBillNo=".$arr['tradeNo']."&InAmount=".$toamount."&ip=".$ip."&uid=".Session::get('uid');
//                echo $url;
                $this->redirect($url);
            }else{
                //下单失败
                return  "<script>alert('请重试');window.location.href='".url('/home/Recharge/recharge')."'</script>";
            }

        }
    }

    //杉德支付页面
    public  function  shande(){
        //var_dump($_GET);
        $data=array();
        $data['order']=$_GET['InMerBillNo'];
        $data['amount']=$_GET['InAmount'];
        $data['charge']=$_GET['charge'];
        $this->assign('data',$data);
        // var_dump($data);
        return $this->fetch('Recharge/shande');
    }

}
?>